package com.hpin.fansea.service;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.ResultBaseBean;
import com.hpin.fansea.bean.ServiceRequest;
import com.hpin.fansea.utils.AbDateUtil;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

import java.util.Calendar;
import java.util.Date;

/**
 * 家政服务
 * Created by Aaron on 15/9/15.
 */
public class HouseKeepingActivity extends BaseActivity {

    private EditText et_declare;
    private TextView tv_order_time;
    private TextView tv_house_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_service_common);

        initWidget();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.housekeeping));

        iv_left.setOnClickListener(this);

        tv_house_address = (TextView) findViewById(R.id.tv_house_address);
        tv_order_time = (TextView) findViewById(R.id.tv_order_time);

        LinearLayout ll_order_time = (LinearLayout) findViewById(R.id.ll_order_time);

        et_declare = (EditText) findViewById(R.id.et_declare);
        //隐藏软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//        et_declare.setau

        Button btn_order = (Button) findViewById(R.id.btn_order);

        ll_order_time.setOnClickListener(this);

        btn_order.setOnClickListener(this);

        TextView tv_price_style = (TextView) findViewById(R.id.tv_price_style);
        tv_price_style.setText(getResources().getString(R.string.housekeeping_price));

        tv_price_style.setOnClickListener(this);

        LinearLayout cherry_llPhone = (LinearLayout) findViewById(R.id.cherry_llPhone);
        LinearLayout azure_tel = (LinearLayout) findViewById(R.id.azure_tel);
        LinearLayout blue_tel = (LinearLayout) findViewById(R.id.blue_tel);
        LinearLayout fragrant_tel = (LinearLayout) findViewById(R.id.fragrant_tel);

        cherry_llPhone.setOnClickListener(this);
        azure_tel.setOnClickListener(this);
        blue_tel.setOnClickListener(this);
        fragrant_tel.setOnClickListener(this);

        //房号
        tv_house_address.setText(sp.getString(Constant.projectName, "") + sp.getString(Constant.buildingNo, "")
                + sp.getString(Constant.unitNo, "") + sp.getString(Constant.houseNo, ""));
        //预约时间
        tv_order_time.setText(AbDateUtil.getStringByFormat(new Date(), AbDateUtil.dateFormatYMD));

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

            case R.id.ll_order_time://选择时间


                Calendar c = Calendar.getInstance();

                new DatePickerDialog(HouseKeepingActivity.this, new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                          int dayOfMonth) {

                        String month = (monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1) + "";
                        String day = dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + "";
//                        String currDay = year + "/" + month + "/" + day;

                        tv_order_time.setText(year + "-" + month + "-" + day);
//
//                        tv_date_attendance.setText(year + "年" + month + "月" + day + "日考勤");
//                        tv_date.setText(year + "年" + month + "月" + day + "日");

//                        if (currDay.equals(lastChoose)) {
//                            return;
//                        } else {
//                            lastChoose = currDay;
//                        }

//                        getAttendanceData(currDay);

                    }

                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
                        c.get(Calendar.DAY_OF_MONTH)).show();

                break;

            case R.id.btn_order://预约

                order();

                break;

            case R.id.tv_price_style://价目表

                Intent mIntent = new Intent(this, PriceListHtActivity.class);
                mIntent.putExtra("style", Constant.FW_JZ);
                startActivity(mIntent);

                break;


            case R.id.cherry_llPhone: // 打电话

                // 提示对话框
                new AlertDialog.Builder(this).setTitle("提示").setMessage("15816098400").setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {

                                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "15816098400"));
                                startActivity(intent);

                            }
                        }).setNegativeButton("取消", null).show();

                break;
            case R.id.azure_tel: // 打电话

                // 提示对话框
                new AlertDialog.Builder(this).setTitle("提示").setMessage("15816098405").setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {

                                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "15816098405"));
                                startActivity(intent);

                            }
                        }).setNegativeButton("取消", null).show();

                break;
            case R.id.blue_tel: // 打电话

                // 提示对话框
                new AlertDialog.Builder(this).setTitle("提示").setMessage("15816098443").setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {

                                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "15816098443"));
                                startActivity(intent);

                            }
                        }).setNegativeButton("取消", null).show();

                break;
            case R.id.fragrant_tel: // 打电话

                // 提示对话框
                new AlertDialog.Builder(this).setTitle("提示").setMessage("15816098409").setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {

                                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "15816098409"));
                                startActivity(intent);

                            }
                        }).setNegativeButton("取消", null).show();

                break;


        }
    }

    private void order() {

        RequestParams params = new RequestParams();

        ServiceRequest request = new ServiceRequest();
        request.token = sp.getString(Constant.token, "");
        request.serviceName = Constant.FW_JZ;
        request.remarks = et_declare.getText().toString().trim();
        request.time = tv_order_time.getText().toString().trim();
        request.address = tv_house_address.getText().toString().trim();

        params.addBodyParameter("data", JSONObject.toJSONString(request));

        HttpUtil.getInstance().setPostRequest("预约中...", this, Constant.WEB_ROOT + Constant.REQUESTSERVICE, params, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ResultBaseBean resultBaseBean = JSONObject.parseObject(str, ResultBaseBean.class);

                    if (Constant.STATUS_SUCESS.equals(resultBaseBean.status)) {

                        showToast("预约成功");
                        finish();

                    } else {
                        showToast(resultBaseBean.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast(Constant.NODATA);
                }
            }
        }, null);

    }
}
